package com.example.demo.service.impl;

import com.example.demo.entity.Safetyinspectionexceptionfile;
import com.example.demo.handle.ExceptionListParseHandler;
import com.example.demo.mapper.SafetyinspectionexceptionfileMapper;
import com.example.demo.service.SafetyinspectionexceptionfileService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.xml.sax.SAXException;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.IOException;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hys
 * @since 2021-07-20
 */
@Service
public class SafetyinspectionexceptionfileServiceImpl extends ServiceImpl<SafetyinspectionexceptionfileMapper, Safetyinspectionexceptionfile> implements SafetyinspectionexceptionfileService {

    @Autowired
    private SafetyinspectionexceptionfileMapper safetyinspectionexceptionfileMapper;

    @Override
    public Integer insertExceptionData(String exceptionXml) {
        //1.获取一个SAXParserFactory的实例对象
        SAXParserFactory factory = SAXParserFactory.newInstance();
        //2.通过factory的newSAXParser()方法获取一个SAXParser类的对象。
        try {
            SAXParser parser = null;
            try {
                parser = factory.newSAXParser();
            } catch (ParserConfigurationException e) {
                e.printStackTrace();
            } catch (SAXException e) {
                e.printStackTrace();
            }
            //创建SAXParserHandler对象
            ExceptionListParseHandler exceptionListParseHandler = new ExceptionListParseHandler();
            try {
                //读取井口信息文件
                parser.parse(exceptionXml, exceptionListParseHandler);

            } catch (IOException e) {
                e.printStackTrace();
            }
            System.out.println("~~~~~共有" + exceptionListParseHandler.getSafetyinspectionexceptionfileArrayList().size() + "个信息");
            // 根据minecode去查找数据库
            for (Safetyinspectionexceptionfile safetyinspectionexceptionfile : exceptionListParseHandler.getSafetyinspectionexceptionfileArrayList()) {
                System.out.println(safetyinspectionexceptionfile.getCsMineCode());
                System.out.println(safetyinspectionexceptionfile.getCsDataTime());
                System.out.println(safetyinspectionexceptionfile.getSsStationCode());
                System.out.println(safetyinspectionexceptionfile.getSsTransducerCode());
                System.out.println(safetyinspectionexceptionfile.getSsStationState());
                System.out.println(safetyinspectionexceptionfile.getSsTransducerState());
                System.out.println(safetyinspectionexceptionfile.getSsSystemAbnormal());
                System.out.println(safetyinspectionexceptionfile.getSsAlarmStime());
                System.out.println(safetyinspectionexceptionfile.getSsAlarmEtime());
                System.out.println(safetyinspectionexceptionfile.getSsDealMeasure());

                System.out.println("----------------------------安全监控信息finish---------------------------------");

                safetyinspectionexceptionfileMapper.insertExceptionData(safetyinspectionexceptionfile.getCsMineCode(),safetyinspectionexceptionfile.getCsDataTime(),safetyinspectionexceptionfile.getSsStationCode(),safetyinspectionexceptionfile.getSsTransducerCode(),safetyinspectionexceptionfile.getSsStationState(),safetyinspectionexceptionfile.getSsTransducerState(),safetyinspectionexceptionfile.getSsSystemAbnormal(),safetyinspectionexceptionfile.getSsAlarmStime(),safetyinspectionexceptionfile.getSsAlarmEtime(),safetyinspectionexceptionfile.getSsDealMeasure());
            }


        } catch (SAXException e) {
            e.printStackTrace();
        }
        return 1;
    }

}
